package com.fingard.dsp.bank.directbank.abc02;

import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;


public class ABC02Deposit extends ABC02Base implements IDeposit {
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAtsDepQry=null;
        if("2".equals(p_req7507.savingType)){//定期
            retAtsDepQry=d_7507_DepQry_2(p_req7507);
        }else if("4".equals(p_req7507.savingType)){//通知
            retAtsDepQry=d_7507_DepQry_4(p_req7507);
        }else {
            retAtsDepQry=new RetAts7507DepQry(p_req7507.reqHeader);
            retAtsDepQry.writeStart();//写返回ATS的统一头部信息
            retAtsDepQry.writeRespCode("-1");
            retAtsDepQry.writeRespInfo("账户存款类型非2或4");
            retAtsDepQry.writeEnd();
        }
        return retAtsDepQry;
    }

    public RetAts7507DepQry d_7507_DepQry_2(Req7507DepQry p_req7507) throws Exception{
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            ABC02Header header = new ABC02Header();
            header.ReqSeqNo = p_req7507.reqHeader.transSeqID;
            header.CCTransCode = "CQRA08";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("DbProv", "");
            xmlWriter.writeElementString("DbAccNo", p_req7507.actNum);
            xmlWriter.writeElementString("DbCur", convertCurToBank(p_req7507.actCur));
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("PrdSeq", "");
            xmlWriter.writeElementString("DbAccName", "");
            xmlWriter.writeElementString("CshDraFlag", "");
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());

            //WriteBankLogLn("请求银行："+xmlWriter.toXmlString());
            //String[] retStrArr=new String[]{"",FileHelper.readFileText("D:\\xmlRspTest\\rsp.txt","utf-8")};
            //WriteBankLogLn("银行响应："+retStrArr[1]);

            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/ap/RespInfo");
                    if (StringHelper.isNullOrEmpty(tmpRetMsg)) {
                        tmpRespInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    }
                    if ("0000".equals(tmpRetCode)) {
                        String mfs=tmpBkReader.getFirstText("/ap/Cmp/RespPrvData1");
                        int fieldNum=13;//根据文档提供字段数
                        String[] mfsArr=mfs.split("\\|",-1);
                        int num=(mfsArr.length-1)/fieldNum;
                        if(num>0){
                            if((mfsArr.length-1)==num*fieldNum){
                                for(int i=0;i<num;i++){
                                    RetAts7507Detail retAts7507Detail = new RetAts7507Detail();
                                    retAts7507Detail.actNum=p_req7507.actNum;//银行账号
                                    retAts7507Detail.actArea=p_req7507.actArea;//账号地区区域码
                                    retAts7507Detail.depositCode="";//定存单号
                                    String endDate=mfsArr[fieldNum*i+7];//截止日期
                                    String term=mfsArr[fieldNum*i+6];//期限
                                    String[] result=getStartDateByEndDateAndTerm(endDate,term);
                                    retAts7507Detail.startDate=result[1];//起始日期
                                    retAts7507Detail.endDate=DateHelper.convertD8toD10(endDate);//截止日期
                                    retAts7507Detail.valueDate=result[1];//起息日期
                                    retAts7507Detail.terms=result[0];//存款期限 (七天`1,一个月`2,三个月`3,六个月`4,一年`5,二年`6,三年`7,五年`8,其它`9)
                                    retAts7507Detail.currencyID=convertCurToATS(mfsArr[fieldNum*i+9]);//币种
                                    retAts7507Detail.noteMoney=mfsArr[fieldNum*i+2];//金额
                                    retAts7507Detail.rate=mfsArr[fieldNum*i+4];//利率(%)
                                    retAts7507Detail.interestMoney="";//利息
                                    retAts7507Detail.depositStates="";//存单状态 (正常`1,冻结`2,未启用`3,已退票`4,销户`5,挂失`7,损益`6,作废`9)
                                    retAts7507Detail.dumpFlag=Integer.parseInt(mfsArr[fieldNum*i+11])+1+"";//到期处理方式 (不转存`1,本息转存`2,本金转存，利息转活期`3)
                                    retAts7507Detail.drawDate="";//上划日期
                                    retAts.writeRetDetail(retAts7507Detail);
                                }
                            }else {
                                tmpRespCode = "-1";
                                tmpRespInfo = "返回数据明细有误";
                            }
                        }
                    }else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                }else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            }else {
                tmpRespCode = "-1";
                tmpRespInfo = retStrArr[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }

    public RetAts7507DepQry d_7507_DepQry_4(Req7507DepQry p_req7507) throws Exception{
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            ABC02Header header = new ABC02Header();
            header.ReqSeqNo = p_req7507.reqHeader.transSeqID;
            header.CCTransCode = "CQRA07";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("DbProv", "");
            xmlWriter.writeElementString("DbAccNo", p_req7507.actNum);
            xmlWriter.writeElementString("DbCur", convertCurToBank(p_req7507.actCur));
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("PrdSeq", "");
            xmlWriter.writeElementString("DbAccName", "");
            xmlWriter.writeElementString("CshDraFlag", "");
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());

            //WriteBankLogLn("请求银行："+xmlWriter.toXmlString());
            //String[] retStrArr=new String[]{"",FileHelper.readFileText("D:\\xmlRspTest\\rsp.txt","utf-8")};
            //WriteBankLogLn("银行响应："+retStrArr[1]);

            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetMsg = tmpBkReader.getFirstText("/ap/RespInfo");
                    if (StringHelper.isNullOrEmpty(tmpRespInfo)) {
                        tmpRespInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    }
                    if ("0000".equals(tmpRetCode)) {
                        String mfs=tmpBkReader.getFirstText("/ap/Cmp/RespPrvData1");
                        int fieldNum=9;//根据文档提供字段数
                        String[] mfsArr=mfs.split("\\|");
                        int num=mfs.length()/fieldNum;
                        if(num>0){
                            if(mfsArr.length==num*fieldNum){
                                for(int i=0;i<num;i++){
                                    RetAts7507Detail retAts7507Detail = new RetAts7507Detail();
                                    retAts7507Detail.actNum=p_req7507.actNum;//银行账号
                                    retAts7507Detail.actArea=p_req7507.actArea;//账号地区区域码
                                    retAts7507Detail.depositCode="";//定存单号
                                    retAts7507Detail.startDate="";//起始日期
                                    retAts7507Detail.endDate="";//截止日期
                                    retAts7507Detail.valueDate="";//起息日期
                                    if("D001".equals(mfsArr[fieldNum*i+8])){
                                        retAts7507Detail.terms="0";//存款期限 (一天`0,七天`1,一个月`2,三个月`3,六个月`4,一年`5,二年`6,三年`7,五年`8,其它`9)
                                    }else if("D007".equals(mfsArr[fieldNum*i+8])){
                                        retAts7507Detail.terms="1";//存款期限
                                    }else {
                                        retAts7507Detail.terms="9";
                                    }
                                    retAts7507Detail.currencyID=convertCurToATS(mfsArr[fieldNum*i+6]);//币种
                                    retAts7507Detail.oriPrincipal=mfsArr[fieldNum*i+2];
                                    retAts7507Detail.noteMoney="";//金额
                                    retAts7507Detail.rate="";//利率(%)
                                    retAts7507Detail.interestMoney="";//利息
                                    retAts7507Detail.depositStates="";//存单状态 (正常`1,冻结`2,未启用`3,已退票`4,销户`5,挂失`7,损益`6,作废`9)
                                    retAts7507Detail.dumpFlag="";//到期处理方式 (不转存`1,本息转存`2,本金转存，利息转活期`3)
                                    retAts7507Detail.drawDate="";//上划日期
                                    retAts.writeRetDetail(retAts7507Detail);
                                }
                            }else {
                                tmpRespCode = "-1";
                                tmpRespInfo = "返回数据明细有误";
                            }
                        }
                    }else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                }else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            }else {
                tmpRespCode = "-1";
                tmpRespInfo = retStrArr[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
